class Solution {
public:
    int maxProduct(vector<int>& nums) {
        //f[i]代表以i位置为结尾的子数组的最大乘积
        //g[i]代表以i位置为结尾的子数组的最小乘积
        int n = nums.size();
        vector<int> f(n+1);
        vector<int> g(n+1);
        f[0] = 1;
        g[0] = 1;
        int ret = -INT_MAX;
        for(int i = 1;i<=n;i++)
        {
           if(nums[i-1]>0)
           {
                f[i] = max(nums[i-1],f[i-1]*nums[i-1]);//对于i这个数字，我可以将其和之前的值乘积
                g[i] = min(nums[i-1],g[i-1]*nums[i-1]);
                ret = max(ret,f[i]); 
           }
           else
           {
                g[i] = min(nums[i-1],f[i-1]*nums[i-1]);
                f[i] = max(nums[i-1],g[i-1]*nums[i-1]);
                 ret = max(ret,f[i]); 
           }
        }
        return ret;
    }
};